package com.twu.biblioteca;

/**
 * Created by siba on 27/09/14.
 */
public class Movie {
    private String name;
    private String year;
    private String director;
    private String rating;
    private boolean isCheckedOut;

    public Movie(){
        this.name = "";
        this.year = "";
        this.director = "";
        this.rating = "";
        this.isCheckedOut = false;
    }

    public Movie(String[] movieDetailsFromUser) {
        this.name = movieDetailsFromUser[0];
        this.director = movieDetailsFromUser[1];
        this.year = movieDetailsFromUser[2];
        this.rating = movieDetailsFromUser[3];
        if(movieDetailsFromUser.length < Constants.MOVIE_ATTRIBUTES.length){
            this.isCheckedOut = false;
        }else
            this.isCheckedOut = Boolean.valueOf(movieDetailsFromUser[4]);

    }

    public void setName(String name){
        this.name = name;
    }
    public void setYear(String year){
        this.year = year;
    }
    public void setDirector(String director){
        this.director = director;
    }
    public void setRating(String rating){
        this.rating = rating;
    }
    public void setIsCheckedOut(boolean isCheckedOut){
        this.isCheckedOut = isCheckedOut;
    }

    public String getName(){
        return this.name;
    }
    public String getDirector(){
        return this.director;
    }
    public String getYear(){
        return this.year;
    }
    public String getRating(){
        return this.rating;
    }
    public boolean getIsCheckedOut(){
        return this.isCheckedOut;
    }

    public static int checkoutMovieFromLibrary(Movie movie){
        Movie libraryMovie = findMovie(movie);

        if(libraryMovie != null && libraryMovie.getIsCheckedOut() == true){
            new BibliotecaMessages(System.out).printMessage(Constants.MOVIE_CURRENTLY_UNAVAILABLE);
            return 0;
        }else if (libraryMovie != null && libraryMovie.getIsCheckedOut() == false){
            new BibliotecaMessages(System.out).printMessage(Constants.MOVIE_CHECKOUT_SUCCESSFUL);
               MovieInformation.toggleMovieCheckedOutInfo(libraryMovie);
            return 1;
        }else{
            new BibliotecaMessages(System.out).printMessage(Constants.MOVIE_CHECKOUT_UNSUCCESSFUL);
            return 0;
        }

    }

    public static Movie findMovie (Movie movie){
        for(Movie libraryMovie: MovieInformation.getLibraryMovies()){
            if (libraryMovie.getName().equalsIgnoreCase(movie.getName()) &&
                    libraryMovie.getDirector().equalsIgnoreCase(movie.getDirector()) &&
                    libraryMovie.getYear().equalsIgnoreCase(movie.getYear())){
                return libraryMovie;
            }
        }
        return null;
    }

}
